const babylon = require("babylon");
const { default: traverse } = require("babel-traverse");
const { default: generator } = require("babel-generator");
const types = require("babel-types");

const state = `let a=1;let b=3;let g = ()=>{
    let c=123;
    for(let j = 0;j<=100;j++){
        console.log(j);
    }
    console.log(1111);
}`;
const ast = babylon.parse(state);
traverse(ast, {
	enter(path) {
		//把数字表达式的值 乘以2
		if (path.node.type == `NumericLiteral`) {
			path.node.value = path.node.value * 2;
		}
		//转换let=>var
		if (path.node.type == `VariableDeclaration` && path.node.kind == "let") {
			path.node.kind = "var";
		}
		//删除console.log()
		if (
			path.node.type == "CallExpression" &&
			path.node.callee.type == "MemberExpression" &&
			path.node.callee.object.name == `console` &&
			path.node.callee.property.name == "log"
		) {
			path.remove();
		}
	}
});

const g = generator(ast, {}, state);

console.log(types);
console.log("aaaaaaaa");

console.log(ast);

console.log(state);

[1, 2, 3, 4, 5].map(x => {
	return {
		id: x
	};
});
